<?php

class UserController extends GxController {

    public $pageTitle = "Users Editor";
    public $backurl = "";
    public $view_css = "/css/views/user.css";

    public $section = "users management";


	public function actionView($id) {

        $this->allowUsers(Array('admin'));
        $this->pageTitle = "User \"".$this->loadModel($id, 'User')->username."\" Details";
        $this->layout = '/layouts/main';
        $this->backurl = "admin";
		$this->render('view', array(
			'model' => $this->loadModel($id, 'User'),
		));
	}



    public function actionCreate() {

    $this->allowUsers(Array('admin'));

    $model=new User;
    $model->new_password = "I dont really CARE 123321*ç&/(_-";

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['User'])) {
        $model->attributes=$_POST['User'];
        if($model->validate()) {
            $model->password = $model->hashPassword($_POST['User']['password']);
        }
        else {
            $model->password = $_POST['User']['password'];
        }

        if($model->save()) {
            CUserLogs::INFO("A new user (".$model->username.") has been created.");
            $this->redirect(array('view', 'id' => $model->id));
        }
        else
            $model->password = $_POST['User']['password'];
    }

    $this->pageTitle = "Add New User";
    $this->layout = '/layouts/main';
    $this->backurl = "admin";
    $this->render('create',array('model'=>$model,));

    }



	public function actionUpdate($id) {

        $this->allowUsers(Array("admin"));

        $model = $this->loadModel($id, 'User');
        $model->new_password = "I dont really CARE 123321*ç&/(_-";
        $old_password = $model->password;

		if (isset($_POST['User'])) {
			$model->setAttributes($_POST['User']);
            $model->password = $_POST['User']['password'];
            if($_POST['User']['password']=='') {
                $model->password = $old_password;
            }
            else if($model->validate()) {
                $model->password = $model->hashPassword($_POST['User']['password']);
            }

            if ($model->save()) {
                CUserLogs::INFO("User ".$model->username." has been updated.");
                $this->redirect(array('view', 'id' => $model->id));
			}
            else {
                $model->password = $_POST['User']['password'];
            }
		}
        $this->pageTitle = "Edit User \"".$model->username."\"";
        $this->layout = '/layouts/main';
        $this->backurl = "admin";
		$this->render('update', array(
				'model' => $model,
				));
	}


	public function actionDelete($id) {

        $this->allowUsers(Array("admin"));

        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $model = $this->loadModel($id, 'User');
            $user = $model->username;
            $model->delete();
            CUserLogs::INFO("The user " . $user . " has been deleted.");

            if (!Yii::app()->getRequest()->getIsAjaxRequest()) {
                $this->redirect(array('admin'));
            }
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}



	public function actionIndex() {
        $this->redirect(array('admin'));
	}



	public function actionAdmin() {

//        Yii::app()->user->setFlash("success","Account has been locked.");
//        Yii::app()->user->setFlash("warning","Account has been locked.");
//        Yii::app()->user->setFlash("error","Account  has been locked.");
//        Yii::app()->user->setFlash("info","Account  has been locked.");

        $this->allowUsers(Array("admin"));

        $model = new User('search');
		$model->unsetAttributes();

		if (isset($_GET['User']))
			$model->setAttributes($_GET['User']);

        if (isset($_GET['pageSize'])) {
            Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);
            unset($_GET['pageSize']);
        }

        //$this->pageTitle = "Users Management";
        $this->layout = '/layouts/main';
        $this->render('admin', array(
			'model' => $model,
		));
	}


    public function actionExcelExport() {

        $this->allowUsers(Array("admin"));

        $model = new User('search');
        $this->layout = 'none';
        $this->render('excelexport', array(
            'model' => $model,
        ));
    }


    public function actionPdfExport() {

        $this->allowUsers(Array("admin"));

        $model = new User('search');
        $this->layout = 'none';
        $this->render('pdfexport', array(
            'model' => $model,
        ));
    }



    public function actionChangepassword() {


        $model = User::model()->findByAttributes(array('username'=>Yii::app()->user->id));
        $this->allowUsers(Array('admin'),Array($model->username)); // All


        $model->setScenario('changePwd');

        if(isset($_POST['User'])){

            $model->attributes = $_POST['User'];
            $valid = $model->validate();

            if($valid){

                $model->password = $model->hashPassword($model->new_password);

                if($model->save()) {
                    CUserLogs::INFO("Account password has been changed.");
                    $this->redirect(array('changepassword', 'msg' => 'successfully changed password'));
                }
                else
                    $this->redirect(array('changepassword','msg'=>'password not changed'));
            }
            else {
                $model->password = $model->new_password;
            }
        }

        $this->pageTitle = "Change Account Password";
        $this->layout = '/layouts/main';
        $this->render('changepassword',array('model'=>$model));
    }



    public function actionUnlock($id) {

        $this->allowUsers(Array("admin"));

            $user = $this->loadModel($id, 'User');
            $user->lock_counter = 0;
            $user->locked = 0;
            $user->save();
            CUserLogs::INFO("The account ".$user->username." has been unlocked.");
            Yii::app()->user->setFlash("success","Account <b>".Yii::app()->user->id."</b> has been unlocked.");
            $this->redirect(array('view','id'=>$user->id));
    }


    public function actionLock($id) {

        $this->allowUsers(Array("admin"));

        // Avoid to lock its own account
            $user = $this->loadModel($id, 'User');
            if($user->username!=Yii::app()->user->id) {
                $user->lock_counter = 0;
                $user->locked = 1;
                $user->save();
                CUserLogs::INFO("The account ".$user->username." has been locked.");
                Yii::app()->user->setFlash("success","Account <b>".$user->username."</b> has been locked.");
                $this->redirect(array('view','id'=>$user->id));
            }
            else {
                Yii::app()->user->setFlash("error","Cannot lock your own account.");
                $this->redirect(array('view','id'=>$id));
            }

    }


    public function actionUpdateUserProfile() {

        $this->pageTitle = "Update User Profile";

        $model = User::model()->findByAttributes(array('username'=>Yii::app()->user->id));
        $this->allowUsers(Array('admin'),Array($model->username)); // All


        if (isset($_POST['User'])) {
            $model->setAttributes($_POST['User']);
            $model->fullname = $_POST['User']['fullname'];
            $model->email = $_POST['User']['email'];
            if($model->validate()) {
                $model->save();
                CUserLogs::INFO("User profile has been updated.");
                Yii::app()->user->setFlash("success","Your account has successfully been updated.");
            }
        }

        $this->layout = '/layouts/main';
        $this->backurl = "";
        $this->render('updateuserprofile', array('model' => $model));
    }



}


